
define([
    "jquery",
    "IO",
    "UPLOADUI",
    "UPLOAD",
    "jqueryUI",
    "bootbox"
], function($,IO,jui,upui,up,bootbox,definebt) {
    $(function () {
        gettable();

        $('#fileupload1').fileupload({
            url: '/upload',
            dataType: 'json',
            autoUpload: true,
            acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
            maxFileSize: 10000000, // 10 MB
            disableImageResize: /Android(?!.*Chrome)|Opera/
                .test(window.navigator.userAgent),
            previewMaxWidth: 150,
            previewMaxHeight: 150,
            previewCrop: true
        }).on('fileuploadadd', function (e, data) {
            document.getElementById('upimgs1').src = '/images/loading.gif';

        }).on('fileuploadprocessalways', function (e, data) {
            //console.log("fileuploadprocessalways",data);
        }).on('fileuploadprogressall', function (e, data) {
            //console.log("fileuploadprogressall",data);
        }).on('fileuploaddone', function (e, data) {
            var alt = $(e.target).attr('alt');
            if (!alt)
                alt = '';
            //console.log("#upimgs1" + alt);
            $("#upimgs1" + alt).attr("src", data._response.result.url);
        });


        $('#fileupload2').fileupload({
            url: '/upload',
            dataType: 'json',
            autoUpload: true,
            acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
            maxFileSize: 10000000, // 10 MB
            disableImageResize: /Android(?!.*Chrome)|Opera/
                .test(window.navigator.userAgent),
            previewMaxWidth: 150,
            previewMaxHeight: 150,
            previewCrop: true
        }).on('fileuploadadd', function (e, data) {
            document.getElementById('upimgs2').src = '/images/loading.gif';

        }).on('fileuploadprocessalways', function (e, data) {
            //console.log("fileuploadprocessalways",data);
        }).on('fileuploadprogressall', function (e, data) {
            //console.log("fileuploadprogressall",data);
        }).on('fileuploaddone', function (e, data) {
            var alt = $(e.target).attr('alt');
            if (!alt)
                alt = '';
            //console.log("#upimgs2" + alt);
            $("#upimgs2" + alt).attr("src", data._response.result.url);
        });

        $(".selectpro").on("change",function(){
            //console.log($(this).val());
            document.reg_testdate.selectcity.innerHTML='请选择市';
            document.reg_testdate.selectcity.options.add(new Option("请选择市","-1"));

            document.reg_testdate.selectarea.innerHTML='请选择区';
            document.reg_testdate.selectarea.options.add(new Option("请选择区","-1"));

            var provinceList = JSON.parse($("#province").val());
            var obj;
            for (obj in provinceList){
                if(provinceList[obj].name == $(this).val()){
                    for (var x in provinceList[obj].cityList){
                        var city = provinceList[obj].cityList;
                        document.reg_testdate.selectcity.options.add(new Option(city[x].name,city[x].code));
                    }
                }
            }
        });

        $(".selectcity").on("change",function(){
            document.reg_testdate.selectarea.innerHTML='请选择区';
            document.reg_testdate.selectarea.options.add(new Option("请选择区","-1"));
            var province = $(".selectpro option:selected").val();
            var provinceList = JSON.parse($("#province").val());
            var obj;
            for (obj in provinceList){
                if(provinceList[obj].name == province){
                    //console.log("测试1 ",$(this).val());
                    //console.log("测试2",provinceList[obj].cityList);
                    var city = provinceList[obj].cityList;
                    for (var x in city){
                        if ($(".selectcity option:selected").text()==city[x].name){
                            var area = city[x].areaList;
                            for (var y in area){
                                document.reg_testdate.selectarea.options.add(new Option(area[y].name,area[y].code));
                                //console.log("城市们",area[y].name);
                            }

                        }

                    }
                }
            }
        });

        //编辑时用到
        $(".selectpro_detail").on("change",function(){
            //console.log($(this).val());
            document.reg_testdate_detail.selectcity_detail.innerHTML='请选择市';
            document.reg_testdate_detail.selectcity_detail.options.add(new Option("请选择市","-1"));

            document.reg_testdate_detail.selectarea_detail.innerHTML='请选择区';
            document.reg_testdate_detail.selectarea_detail.options.add(new Option("请选择区","-1"));

            var provinceList = JSON.parse($("#province").val());
            var obj;
            for (obj in provinceList){
                if(provinceList[obj].name == $(this).val()){
                    for (var x in provinceList[obj].cityList){
                        var city = provinceList[obj].cityList;
                        document.reg_testdate_detail.selectcity_detail.options.add(new Option(city[x].name,city[x].code));
                    }
                }
            }
        });

        $(".selectcity_detail").on("change",function(){
            document.reg_testdate_detail.selectarea_detail.innerHTML='请选择区';
            document.reg_testdate_detail.selectarea_detail.options.add(new Option("请选择区","-1"));
            var province = $(".selectpro_detail option:selected").val();
            var provinceList = JSON.parse($("#province").val());
            var obj;
            for (obj in provinceList){
                if(provinceList[obj].name == province){
                    //console.log("测试1 ",$(this).val());
                    //console.log("测试2",provinceList[obj].cityList);
                    var city = provinceList[obj].cityList;
                    for (var x in city){
                        if ($(".selectcity_detail option:selected").text()==city[x].name){
                            var area = city[x].areaList;
                            for (var y in area){
                                document.reg_testdate_detail.selectarea_detail.options.add(new Option(area[y].name,area[y].code));
                                //console.log("城市们",area[y].name);
                            }

                        }

                    }
                }
            }
        });

        $(".btnAdd").click(function(){

            var province_id = $(".selectpro option:selected").attr('data-id');
            var city_id = $(".selectcity option:selected").val();
            var area_id = $(".selectarea option:selected").val();
            var birth=$("#birth").val();                                 //生日
            var valid=$("#valid option:selected").val();                 //认证情况

            var health_creadential=$("#health_creadential option:selected").val();//有无健康证
            var registered_residence=$("#registered_residence").val();//户籍
            var degree=$("#degree").val(); //学历
            var start_work_time=$("#start_work_time").val(); //开始工作时间
            var gongzhong=$("#gongzhong option:selected").val(); //工种

            var weight=$("#weight").val(); //体重
            var height=$("#height").val(); //身高
            var cost=$("#cost").val(); //服务费用
            var zodiac=$("#zodiac option:selected").val(); //生肖
            var phone=$("#phone").val(); //联系电话

            var idcard=$("#idcard").val(); //身份证号
            var company_id=$("#company_id_hidden").val(); //所属公司
            var credential_pic=$("#upimgs1").attr("src"); //手持证件照
            var credential_pic_r=$("#upimgs2").attr("src"); //反面照
            var goodat=$("#goodat").val(); //擅长内容

            var desc=$("#desc").val(); //描述
            var gender=$("#gender option:selected").val();//性别
            var first_name=$("#first_name").val(); //姓
            var second_name=$("#second_name").val();//名

            var params={
                area_id:area_id,
                province_id:province_id,
                city_id:city_id,
                birth:birth,
                valid:valid,

                health_creadential:health_creadential,
                registered_residence:registered_residence,
                degree:degree,
                start_work_time:start_work_time,
                gongzhong:gongzhong,

                weight:weight,
                height:height,
                cost:cost,
                zodiac:zodiac,
                phone:phone,

                idcard:idcard,
                company_id:company_id,
                credential_pic:credential_pic,
                credential_pic_r:credential_pic_r,
                goodat:goodat,

                desc:desc,
                gender:gender,
                first_name:first_name,
                second_name:second_name
            };
            if(first_name.length<=0){
                alert('请填写姓');
                return;
            }
            if(second_name.length<=0){
                alert('请填写名');
                return;
            }
            if(birth.length<=0){
                alert('请选择出生日期');
                return;
            }
            if(registered_residence.length<=0){
                alert('请填写户籍');
                return;
            }
            if(degree.length<=0){
                alert('请填写学历');
                return;
            }
            if(start_work_time.length<=0){
                alert('请选择工作开始时间');
                return;
            }
            if(weight.length<=0){
                alert('请填写体重');
                return;
            }
            if(height.length<=0){
                bootbox.alert('请填写身高');
                return;
            }
            if(phone.length<=0){
                alert('请填写电话');
                return;
            }
            if(cost.length<=0){
                alert('请填写费用');
                return;
            }
            if(idcard.length<=0){
                alert('请填写身份证');
                return;
            }
            if(credential_pic.length<=0){
                alert('请上传正面照');
                return;
            }
            if(credential_pic_r.length<=0){
                alert('请上传反面照');
                return;
            }
            if(goodat.length<=0){
                alert('请填写擅长内容');
                return;
            }
            if(desc.length<=0){
                alert('请填写描述内容');
                return;
            }
            if(zodiac==-1){
                alert('请选择生肖');
                return;
            }
            if(area_id==-1){
                alert('请选择服务区域');
                return;
            }
            IO.post('/api/baby_sister',params,function(d){
                if(d.code==0){
                    bootbox.alert('新增成功');
                    $('#myModal').modal('hide');
                    gettable();
                }
                else {
                    bootbox.alert('新增失败');
                    $('#myModal').modal('hide');
                }
            })
        });
        $('#myModal').on('hide.bs.modal', function () {
            $("#birth").val("");
            $("#degree").val("");
            $("#weight").val("");
            $("#height").val("");
            $("#height").val("");//户籍
            $("#cost").val("");
            $("#phone").val("");
            $("#idcard").val("");
            $("#desc").val("");
            $("#goodat").val("");
            $("#first_name").val("");
            $("#second_name").val("");
            $("#company_id").val("-1");
            $("#upimgs1").attr("src",'/images/ylm.png');
            $("#upimgs2").attr("src",'/images/ylm.png');
        })

        $("#btnEdit").click(function(){
            $("#btnEdit").hide();
            $("#btnTrueEdit").show();
            $('#myModaldetail select').attr("disabled",false);
            $("#myModaldetail input").removeAttr("readOnly");
            $("#myModaldetail textarea").removeAttr("readOnly");
        });

        //修改服务人员
        $("#btnTrueEdit").click(function(){
            var province_id = $(".selectpro_detail option:selected").attr('data-id');
            var city_id = $(".selectcity_detail option:selected").val();
            var area_id = $(".selectarea_detail option:selected").val();
            var birth=$("#birth_detail").val();                                 //生日
            var valid=$("#valid_detail option:selected").val();                 //认证情况

            var health_creadential=$("#health_creadential_detail option:selected").val();//有无健康证
            var registered_residence=$("#registered_residence_detail").val();//户籍
            var degree=$("#degree_detail").val(); //学历
            var start_work_time=$("#start_work_time_detail").val(); //开始工作时间
            var gongzhong=$("#gongzhong_detail option:selected").val(); //工种

            var weight=$("#weight_detail").val(); //体重
            var height=$("#height_detail").val(); //身高
            var cost=$("#cost_detail").val(); //服务费用
            var zodiac=$("#zodiac_detail option:selected").val(); //生肖
            var phone=$("#phone_detail").val(); //联系电话

            var idcard=$("#idcard_detail").val(); //身份证号
            var company_id=$("#company_id_hidden").val(); //所属公司
            var credential_pic=$("#upimgs1_detail").attr("src"); //手持证件照
            var credential_pic_r=$("#upimgs2_detail").attr("src"); //反面照
            var goodat=$("#goodat_detail").val(); //擅长内容

            var desc=$("#desc_detail").val(); //描述
            var gender=$("#gender_detail option:selected").val();//性别
            var first_name=$("#first_name_detail").val(); //姓
            var second_name=$("#second_name_detail").val();//名

            var params={
                area_id:area_id,
                province_id:province_id,
                city_id:city_id,
                birth:birth,
                valid:valid,

                health_creadential:health_creadential,
                registered_residence:registered_residence,
                degree:degree,
                start_work_time:start_work_time,
                gongzhong:gongzhong,

                weight:weight,
                height:height,
                cost:cost,
                zodiac:zodiac,
                phone:phone,

                idcard:idcard,
                company_id:company_id,
                credential_pic:credential_pic,
                credential_pic_r:credential_pic_r,
                goodat:goodat,

                desc:desc,
                gender:gender,
                first_name:first_name,
                second_name:second_name,
                baby_sister_id:baby_sister_id_edit
            };
            if(first_name.length<=0){
                alert('请填写姓');
                return;
            }
            if(second_name.length<=0){
                alert('请填写名');
                return;
            }
            if(birth.length<=0){
                alert('请选择出生日期');
                return;
            }
            if(registered_residence.length<=0){
                alert('请填写户籍');
                return;
            }
            if(degree.length<=0){
                alert('请填写学历');
                return;
            }
            if(start_work_time.length<=0){
                alert('请选择工作开始时间');
                return;
            }
            if(weight.length<=0){
                alert('请填写体重');
                return;
            }
            if(height.length<=0){
                alert('请填写身高');
                return;
            }
            if(phone.length<=0){
                alert('请填写电话');
                return;
            }
            if(cost.length<=0){
               alert('请填写费用');
                return;
            }
            if(idcard.length<=0){
                alert('请填写身份证');
                return;
            }
            if(credential_pic.length<=0){
                alert('请上传正面照');
                return;
            }
            if(credential_pic_r.length<=0){
                alert('请上传反面照');
                return;
            }
            if(goodat.length<=0){
                alert('请填写擅长内容');
                return;
            }
            if(desc.length<=0){
                alert('请填写描述内容');
                return;
            }
            if(zodiac==-1){
                alert('请选择生肖');
                return;
            }
            if(area_id==-1){
               alert('请选择服务区域');
                return;
            }
            IO.post('/api/baby_sister_edit',params,function(d){
                if(d.code==0){
                    bootbox.alert('修改成功');
                    $('#myModaldetail').modal('hide');
                    gettable();
                }
                else {
                    bootbox.alert('修改失败');
                    $('#myModaldetail').modal('hide');
                }
            })
        });

    });
    function gettable(){

        var grid;
        //每列数据列性
        var columns = [
            {
                title: '姓名',
                dataIndex: 'first_name',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].first_name +data[datarow].second_name+ "</span>";
                }
            },
            {
                title: '认证情况',
                dataIndex: 'company_name',
                dataSource: function (data, datarow, gridobj, current_column) {
                    if(data[datarow].valid==1){
                        return "<span>" + '已认证' + "</span>";
                    }
                    else{
                        return "<span>" + '未认证' + "</span>";
                    }

                }
            },
            {
                title: '户籍',
                dataIndex: 'registered_residence',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].registered_residence + "</span>";

                }
            },
            {
                title: '学历',
                dataIndex: 'degree',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].degree + "</span>";

                }
            },
            {
                title: '服务',
                dataIndex: 'service',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + "月嫂" + "</span>";

                }
            },
            {
                title: '经验',
                dataIndex: 'workyear',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow].workyear + "</span>";

                }
            },
            {
                title: '所属公司',
                dataIndex: 'company_name',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<span>" + data[datarow]['company'].company_name + "</span>";
                }
            },
            {
                title: '操作',
                dataIndex: 'action',
                dataSource: function (data, datarow, gridobj, current_column) {
                    return "<a href='#' baby_sister_id="+data[datarow].baby_sister_id+" class='details'><span style='color: #6ab7ad;'>" + "查看详情" + "</span></a>";
                }
            }
        ];
        //数组初始化

        function dataInit() {

            grid = new Grid("/api/baby_sister/list", $("#table_my_toast"), $("#paging_my_toast"), 1, 10, columns, get_input_param(), 10);

            //初始化
            grid.Init();
        }

        //获取输入框内容
        function get_input_param() {
            var company_id=$("#company_id_hidden").val();
            var param="company_id="+company_id;
            return param;
        }
        dataInit();
    }

    var baby_sister_id_edit="";
    //服务人员详情
    $(document).on('click','.details',function(){
        var baby_sister_id=$(this).attr('baby_sister_id');
        baby_sister_id_edit=baby_sister_id;
        var parmas={
            baby_sister_id:baby_sister_id
        };

        IO.get('/api/baby_sister_detail',parmas,function(d){
            if(d.code==0){

                $("#btnEdit").show();
                $("#btnTrueEdit").hide();
                $('#myModaldetail select').attr("disabled",true);
                $("#myModaldetail input").attr("readOnly",'readOnly');
                $("#myModaldetail textarea").attr("readOnly",'readOnly');


                var data= d.data;
                $("#first_name_detail").val(data.first_name);
                $("#second_name_detail").val(data.second_name);
                $("#birth_detail").val(data.birth.substring(0,10));//日期
                $("#valid_detail").val(data.valid);//认证
                //$("#health_creadential_detail option[value='"+data.health_creadential+"']").attr("selected",true);//健康认证
                $("#health_creadential_detail").val(data.health_creadential);//健康认证
                $("#registered_residence_detail").val(data.registered_residence);//户籍
                $("#degree_detail").val(data.degree);//学历
                $("#unit_detail").text(data.serviceCount);//服务次数
                $("#score_detil").text(data.score);//服务评分
                $("#start_work_time_detail").val(data.start_work_time.substring(0,10));//服务评分
                $("#weight_detail").val(data.weight);//体重
                $("#height_detail").val(data.height);//身高
                $("#cost_detail").val(data.cost);//费用
                //$("#zodiac_detail option[value='"+data.zodiac+"']").attr("selected",true);//生肖
                $("#zodiac_detail").val(data.zodiac);//生肖
                $("#phone_detail").val(data.phone);//联系电话
                //$("#gender_detail option[value='"+data.gender+"']").attr("selected",true);//性别
                $("#gender_detail").val(data.gender);//性别
                $("#idcard_detail").val(data.idcard);//身份证
                //$("#company_id_detail option[value='"+data.company_id+"']").attr("selected",true);//公司

                $("#company_id_detail").val(data.company_id);

                $("#upimgs1_detail").attr('src',data.credential_pic);
                $("#upimgs2_detail").attr('src',data.credential_pic_r);
                var area_id=data.area_id;
                var province_id=data.province_id;
                var city_id=data.city_id;
                Get_detail(province_id,city_id,area_id);

                $("#goodat_detail").val(data.goodat);//
                $("#desc_detail").val(data.description);//

                $('#myModaldetail').modal('show');
            }
            else {

            }
        });
    });

    function Get_detail(province_id,city_id,area_id){
        var provinceList = JSON.parse($("#province").val());
        var province_id = province_id;
        var city_id = city_id;
        var area_id = area_id;
        //console.log('province_id',province_id);
        //console.log('city_id',city_id);
        //console.log('area_id',area_id);
        document.reg_testdate_detail.selectcity_detail.innerHTML='请选择市';
        document.reg_testdate_detail.selectcity_detail.options.add(new Option("请选择市","请选择市"));
        for (var obj in provinceList){
            if(provinceList[obj].code == province_id){
                for (var x in provinceList[obj].cityList){
                    var city = provinceList[obj].cityList;
                    document.reg_testdate_detail.selectcity_detail.options.add(new Option(city[x].name,city[x].code));
                }
            }
        }

        //获取区的下拉框选项
        document.reg_testdate_detail.selectarea_detail.innerHTML='请选择区';
        document.reg_testdate_detail.selectarea_detail.options.add(new Option("请选择区","请选择区"));
        for (var  obj in provinceList){
            if(provinceList[obj].code == province_id){
                //console.log("测试1 ",$(this).val());
                //console.log("测试2",provinceList[obj].cityList);
                var city = provinceList[obj].cityList;
                for (var x in city){
                    if (city[x].code == city_id){
                        var area = city[x].areaList;
                        for (var y in area){
                            document.reg_testdate_detail.selectarea_detail.options.add(new Option(area[y].name,area[y].code));
                            //console.log("城市们",area[y].name);
                        }
                    }
                }
            }
        }
        //下拉框选中该医院所在的省市区
        var provinces = $(".selectpro_detail");
        var citys = $(".selectcity_detail");
        var areas = $(".selectarea_detail");
        for(var x=1;x<provinces[0].length;x++ ){
            if($(provinces[0][x]).attr('data-id')==province_id){
                provinces[0][x].selected = true;
                continue;
            }
        }

        for(var y=1;y<citys[0].length;y++ ){
            if(citys[0][y].value==city_id){
                citys[0][y].selected = true;
                continue;
            }
        }
        for(var z=1;z<areas[0].length;z++ ){
            if(areas[0][z].value==area_id){
                areas[0][z].selected = true;
                continue;
            }
        }

    };
});